package com.example.index.arch.mvvm

import android.app.Activity
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import com.example.index.R
import com.example.index.arch.mvvm.model.LoginViewModel
import com.example.index.databinding.ActivityMvvmBinding
import dagger.android.AndroidInjector
import dagger.android.DispatchingAndroidInjector
import dagger.android.HasActivityInjector
import es.dmoral.toasty.Toasty
import javax.inject.Inject

class MVVMActivity : AppCompatActivity(), ActivityInjectable, HasActivityInjector {


    @Inject
    lateinit var dispatchingAndroidInjector: DispatchingAndroidInjector<Activity>

    override fun activityInjector(): AndroidInjector<Activity> = dispatchingAndroidInjector


    @Inject
    lateinit var loginViewModel: LoginViewModel

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val binding =
            DataBindingUtil.setContentView<ActivityMvvmBinding>(this, R.layout.activity_mvvm)
        binding.loginViewModel = loginViewModel

        loginViewModel.loginResult.observe(this, Observer<String> {
            Toasty.info(this@MVVMActivity, it.toString(), Toasty.LENGTH_SHORT).show()
        })
    }
}
